package client.global.commlib.widget

import android.content.Context
import android.util.AttributeSet
import android.widget.LinearLayout
import androidx.core.view.children
import androidx.fragment.app.Fragment
import androidx.fragment.app.Fragment.instantiate
import androidx.fragment.app.FragmentActivity
import client.global.commlib.R

class HomeNavHost @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {

    val contentId: Int

    val set: HashMap<String, Fragment?> by lazy {
        HashMap<String, Fragment?>()
    }
    lateinit var setKeys: MutableList<String>

    var currString = ""

    @Volatile
    var isInit = false

    init {
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.homeNavStyle)
        contentId = typedArray.getResourceId(R.styleable.homeNavStyle_content_id, -1)
        typedArray.recycle()
    }

    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        super.onLayout(changed, l, t, r, b)

        if (!isInit) {
            isInit = true
            for ((i, child) in children.withIndex()) {
                if (i == 0)
                    child.isSelected = true
                child.setOnClickListener {
                    if (it.isSelected)
                        return@setOnClickListener
                    doTabChanged(setKeys.get(i))
                    for (v in children) {
                        if (v == it) {
                            v.isSelected = true
                        } else {
                            v.isSelected = false
                        }
                    }
                }
            }
        }

    }

    fun setUp(set: MutableList<String>) {
        this.setKeys = set
        clearOld()
        doTabChanged(setKeys.get(0))
    }


    fun clearOld() {
        val transaction = (context as FragmentActivity).supportFragmentManager.beginTransaction()
        val fragments =
            (context as FragmentActivity).supportFragmentManager.fragments
        if (transaction == null || fragments == null || fragments.size == 0) return
        var doCommit = false
        for (fragment in fragments) {
            if (fragment != null) {
                transaction.remove(fragment)
                doCommit = true
            }
        }
        if (doCommit) transaction.commitNow()
    }

    private fun doTabChanged(newKey: String) {
        val ft = (context as FragmentActivity).supportFragmentManager.beginTransaction()
        val oldNavButton = set.get(currString)
        if (oldNavButton != null) {
            if (oldNavButton != null) {
                ft.detach(oldNavButton)
            }
        }
        currString = newKey
        val newNavButton = set.get(newKey)
        if (newNavButton == null) {
            val fragment =
                instantiate(
                    context,
                    newKey, null
                ) as Fragment
            ft.add(contentId, fragment, newKey)
            set.put(newKey, fragment)
        } else {
            ft.attach(newNavButton)
        }

        ft.commit()
    }
}